﻿Public Class ReaderSettings
    Public WithEvents oWindowList As OpenedWindowList = OpenedWindowList.GetList()
    Private WithEvents oWindow As OpenedWindowClass
    Dim v_isdirty As Boolean = False

#Region "Control Events"

    Private Sub buttonSave_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles buttonSave.Click
        If v_isdirty Then
            My.Settings.localLocation = textboxBackupFilesLocation.Text
            My.Settings.remoteLocation = textboxReaderFilesLocation.Text
            My.Settings.removeFilesonReader = cbRemoveFilesFromReader.IsChecked

            My.Settings.readerFirstRowHeader = cbReaderFileHeaderFirstRow.IsChecked
            My.Settings.readerRFIDNumberLength = textboxRFIDNumLength.Text
            My.Settings.readerRowDelimiter = textboxReaderDelimiter.Text
            My.Settings.readerFileExtension = textboxReaderFileExtension.Text
            My.Settings.readerRowFormat = textboxReaderRowFormat.Text
            My.Settings.readerCreateAssetData = cbReaderCreateAssetData.IsChecked

            My.Settings.Save()
            v_isdirty = False
            SetWindowProperties(False)
            MessageBox.Show("Data has been saved successfully.", "ATS", MessageBoxButton.OK, MessageBoxImage.Information)
        Else
            MessageBox.Show("Nothing saved, values were not changed.", "ATS", MessageBoxButton.OK, MessageBoxImage.Information)
        End If
    End Sub
    Private Sub buttonRefresh_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles buttonRefresh.Click
        If v_isdirty = True Then

            If MessageBox.Show("There are unsaved entries. Do you really want refresh the data?", "Refresh", MessageBoxButton.YesNo, MessageBoxImage.Warning) = MessageBoxResult.Yes Then
                LoadDefaults()
            End If
        Else
            LoadDefaults()

        End If
    End Sub

    Private Sub buttonBrowseReader_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles buttonBrowseReader.Click
        Dim ownedWindow As New BrowseDevice
        Try
            ownedWindow.Path = textboxReaderFilesLocation.Text
            ownedWindow.ShowDialog()

            If ownedWindow.OkCancel = "OK" Then
                textboxReaderFilesLocation.Text = ownedWindow.Path
            Else
                ownedWindow.Close()
                ownedWindow = Nothing
            End If
        Catch ex As Exception
            ownedWindow.Close()
            MsgBox("Please check if your device is connected.", MsgBoxStyle.OkOnly, "Connecting to device...")
        End Try
    End Sub

    Private Sub buttonBrowseBackup_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles buttonBrowseBackup.Click
        Dim dlg As New System.Windows.Forms.FolderBrowserDialog
        dlg.SelectedPath = textboxBackupFilesLocation.Text
        If dlg.ShowDialog() = Forms.DialogResult.OK Then
            textboxBackupFilesLocation.Text = dlg.SelectedPath & "\"
        Else
            textboxBackupFilesLocation.Text = My.Settings.localLocation
        End If
    End Sub
    Private Sub textboxBackupFilesLocation_TextChanged(ByVal sender As Object, ByVal e As System.Windows.Controls.TextChangedEventArgs) Handles textboxBackupFilesLocation.TextChanged
        v_isdirty = True
        SetWindowProperties(True)
    End Sub
    Private Sub textboxReaderFilesLocation_TextChanged(sender As Object, e As System.Windows.Controls.TextChangedEventArgs) Handles textboxReaderFilesLocation.TextChanged
        v_isdirty = True
        SetWindowProperties(True)
    End Sub

    Private Sub cbRemoveFilesFromReader_Checked(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles cbRemoveFilesFromReader.Checked, cbRemoveFilesFromReader.Unchecked
        v_isdirty = True
        SetWindowProperties(True)
    End Sub
    Private Sub cbReaderFileHeaderFirstRow_Checked(sender As Object, e As System.Windows.RoutedEventArgs) Handles cbReaderFileHeaderFirstRow.Checked, cbReaderFileHeaderFirstRow.Unchecked
        v_isdirty = True
        SetWindowProperties(True)
    End Sub
    Private Sub textboxRFIDNumLength_TextChanged(sender As Object, e As System.Windows.Controls.TextChangedEventArgs) Handles textboxRFIDNumLength.TextChanged
        v_isdirty = True
        SetWindowProperties(True)
    End Sub
    Private Sub textboxReaderDelimiter_TextChanged(sender As Object, e As System.Windows.Controls.TextChangedEventArgs) Handles textboxReaderDelimiter.TextChanged
        v_isdirty = True
        SetWindowProperties(True)
    End Sub
    Private Sub textboxReaderFileExtension_TextChanged(sender As Object, e As System.Windows.Controls.TextChangedEventArgs) Handles textboxReaderFileExtension.TextChanged
        v_isdirty = True
        SetWindowProperties(True)
    End Sub
    Private Sub textboxReaderRowFormat_TextChanged(sender As Object, e As System.Windows.Controls.TextChangedEventArgs) Handles textboxReaderRowFormat.TextChanged
        v_isdirty = True
        SetWindowProperties(True)
    End Sub
    Private Sub cbReaderCreateAssetData_Checked(sender As Object, e As System.Windows.RoutedEventArgs) Handles cbReaderCreateAssetData.Checked, cbReaderCreateAssetData.Unchecked
        v_isdirty = True
        SetWindowProperties(True)
    End Sub
#End Region

#Region "Window Events"

    Private Sub ReaderSettings_Closed(sender As Object, e As System.EventArgs) Handles Me.Closed
        Dim ied As IEnumerable(Of OpenedWindowClass) = From d In oWindowList Where d.WindowID = Me.Name
        oWindowList.Remove(ied.First)
        If Application.Current.Windows.Count = 1 Then
            Application.Current.Windows(0).Activate()
        End If
    End Sub

    Private Sub ReaderSettings_Closing(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles Me.Closing
        If v_isdirty Then
            If MessageBox.Show("There are unsaved entries. Do you really want to exit?", "Exit", MessageBoxButton.YesNo, MessageBoxImage.Warning) = MessageBoxResult.No Then
                e.Cancel = True
            End If
            SetWindowProperties(False)
        End If
    End Sub

    Private Sub ReaderSettings_Loaded(sender As Object, e As System.Windows.RoutedEventArgs) Handles Me.Loaded

        LoadDefaults()
        v_isdirty = False
    End Sub

#End Region

#Region "Support Procedures"
    Private Sub SetWindowProperties(dirty As Boolean)
        If dirty Then
            Me.Title &= IIf(Not Me.Title.ToUpper.Contains("UNSAVED"), " (unsaved)", "")
        Else
            Me.Title = IIf(Title.ToUpper.Contains("UNSAVED"), Me.Title.Substring(0, Me.Title.Length - 10), Me.Title)
        End If

        For itemIndex = 0 To oWindowList.Count - 1
            If oWindowList(itemIndex).WindowID = Me.Name Then
                oWindowList(itemIndex).WindowName = Me.Title
                oWindowList(itemIndex).WindowIsDirty = dirty
            End If
        Next

    End Sub

    Private Sub LoadDefaults()
        textboxBackupFilesLocation.Text = My.Settings.localLocation
        textboxReaderFilesLocation.Text = My.Settings.remoteLocation
        cbRemoveFilesFromReader.IsChecked = My.Settings.removeFilesonReader

        cbReaderFileHeaderFirstRow.IsChecked = My.Settings.readerFirstRowHeader
        textboxRFIDNumLength.Text = My.Settings.readerRFIDNumberLength
        textboxReaderDelimiter.Text = My.Settings.readerRowDelimiter
        textboxReaderFileExtension.Text = My.Settings.readerFileExtension
        textboxReaderRowFormat.Text = My.Settings.readerRowFormat
        cbReaderCreateAssetData.IsChecked = My.Settings.readerCreateAssetData

        SetWindowProperties(False)
    End Sub



#End Region
    
    
    
    
    
    
End Class
